"""
Case Type   : 资源池化集群系统支持归档备份
Case Name   : 设置archive_timeout=30,执行tpcc业务，生成归档
Create At   : 2025/04/19
Owner       : @kyleze
Description :
    1.设置archive_mode=on
    2.设置archive_dest
    3.设置archive_timeout=30
    4.执行tpcc业务，等待30s
    5.查看归档文件是否存在
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.生成归档文件
History     :
"""

import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "dms_res" not in Common().get_sh_result(
        Node("PrimaryDbUser"),
        f"source {macro.DB_ENV_PATH};cm_ctl query -Cvidp"), "非资源池化不执行")
class SharedStorageAchiveXlog(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"----{os.path.basename(__file__)} start----")
        self.constant = Constant()
        self.com = Common()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.primary_node = Node("PrimaryDbUser")
        self.pri_root_node = Node(node="PrimaryRoot")
        self.archive_dest = os.path.join(macro.DB_BACKUP_PATH,
                                         "archive_dir_010/")
        self.default_archive_mode = \
            self.com.show_param("archive_mode")
        self.default_archive_dest = \
            self.com.show_param("archive_dest")
        self.default_archive_timeout = \
            self.com.show_param("archive_timeout")

    def test_archive_xlog(self):
        step = "--step2:设置archive_mode=on expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode=on")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step2:设置archive_dest= expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.archive_dest}'")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step3:执行pg_switch_xlog expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_timeout=30")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step4:执行tpcc业务，等待30s expect:成功--"
        self.logger.info(step)
        tpcc_thread = ComThread(
            self.com.start_tpcc, args=(self.primary_node, macro.TPCC_PATH))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()
        time.sleep(20)

        step = "--step5:查看归档文件是否存在 expect:不生成归档文件--"
        self.logger.info(step)
        cmd = f"ls {self.archive_dest}"
        self.logger.info(cmd)
        ls_archive = self.primary_node.sh(cmd).result()
        self.logger.info(ls_archive)
        archive_bool = 'No such file or directory' not in ls_archive and len(
            ls_archive.split()) > 0
        self.assertTrue(archive_bool, "执行失败:" + step)

    def tearDown(self):
        self.logger.info("----清理环境----")
        guc_res1 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode={self.default_archive_mode}")
        self.logger.info(guc_res1)
        guc_res2 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.default_archive_dest}'")
        self.logger.info(guc_res2)
        guc_res3 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_timeout={self.default_archive_timeout}")
        self.logger.info(guc_res3)
        self.assertTrue(guc_res1, "设置参数失败")
        self.assertTrue(guc_res2, "设置参数失败")
        self.assertTrue(guc_res3, "设置参数失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
